import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebServlet("/logout")  // 映射到/logout路径
public class LogoutServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        // 1. 销毁会话
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();  // 清除所有会话数据
        }

        // 2. 重定向到登录页
        response.sendRedirect(request.getContextPath() + "/login");
    }
}
